const Controller = require('egg').Controller;

class HomeController extends Controller {
    //获取经纪人数据
    async broker() {
        const { ctx, app } = this

        const { value } = ctx.request.body

        try {
            const data = await app.mysql.query(`select * from 经纪人 where name like '%${value}%'`)

            ctx.body = {
                code: 0,
                msg: '请求成功',
                data
            }
        } catch (error) {
            ctx.body = {
                msg: error
            }
        }
    }

    //获取经纪人详情数据
    async brokerList() {
        const { ctx, app } = this

        const data = await app.mysql.query('select * from 经纪人')

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //获取消息数据
    async mseeage() {
        const { ctx, app } = this

        const data = await app.mysql.query('select * from 消息')

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //删除消息
    async mesDetete() {
        const { ctx, app } = this

        const { id } = ctx.request.body

        await app.mysql.delete('消息', { id })

        ctx.body = {
            code: 0,
            msg: '请求成功'
        }
    }

    //获取二手房数据
    async getResoldHome() {
        const { ctx, app } = this

        const { value } = ctx.request.body

        try {
            const data = await app.mysql.query(`select * from 二手房 where title like '%${value}%'`)

            ctx.body = {
                code: 0,
                msg: '请求成功',
                data
            }
        } catch (error) {
            ctx.body = {
                msg: error
            }
        }
    }

    //获取求租/购数据
    async getWanted() {
        const { ctx, app } = this

        const data = await app.mysql.query('select * from 房屋求租')

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //获取楼市圈数据
    async homeMarket() {
        const { ctx, app } = this

        const data = await app.mysql.query('select * from 楼市圈')

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //房产类型筛选
    async chooseType() {
        const { ctx, app } = this

        const { value } = ctx.request.body

        console.log(ctx.request.body);

        const data = await app.mysql.query(`select * from 房屋求租 where type like '%${value}%'`)

        if (value === '房产类型') {
            ctx.body = {
                code: 0,
                msg: '请求成功',
                data
            }
        } else {
            ctx.body = {
                code: 500,
                data
            }
        }
    }

    //区域筛选
    async chooseAddress() {
        const { ctx, app } = this

        const { value } = ctx.request.body

        const data = await app.mysql.query(`select * from 房屋求租 where address like '%${value}%'`)

        if (value === '区域选择') {
            ctx.body = {
                code: 0,
                msg: '请求成功',
                data
            }
        } else {
            ctx.body = {
                code: 500,
                data
            }
        }
    }

    //新房区域筛选
    async newHomeAddress() {
        const { ctx, app } = this

        const { value } = ctx.request.body

        const data = await app.mysql.query(`select * from 二手房 where address like '%${value}%'`)

        if (value === '区域') {
            ctx.body = {
                code: 0,
                msg: '请求成功',
                data
            }
        } else {
            ctx.body = {
                code: 500,
                data
            }
        }
    }

    //楼市圈删除
    async marketDelete() {
        const { ctx, app } = this

        const { id } = ctx.request.body

        await app.mysql.delete('楼市圈', { id })

        ctx.body = {
            code: 0,
            msg: '请求成功'
        }
    }
}

module.exports = HomeController;
